
package shop.maytinh.controller;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.AliasToBeanResultTransformer;
import org.hibernate.transform.Transformers;

import shop.maytinh.util.HibernateUtil;
import shop.maytinh.model.*;

 

public class MemberManager extends HibernateUtil {
	
	List<Member> listmember;
	public Member member; 
	
	public List<Member> getlistmember(){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		listmember = null;
	    session.beginTransaction();			
	    try{
	    	listmember = (List<Member>)session.createQuery("from Member").list();	    	
			if(listmember==null)
				return null;
		}
		catch(Exception e){
			 e.printStackTrace();
			 session.getTransaction().rollback();
		}
	    session.getTransaction().commit();
		return listmember;
	}	

	public Member getUserByUserPass(String user, String pass ) throws Exception {
        try {
        	Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    		Member member = null;
    	    session.beginTransaction();
    	    member = (Member) session.createCriteria(Member.class)
                    .add(Restrictions.eq("user",user))
                    .add(Restrictions.eq("pass",pass))
                    .uniqueResult();
    	    session.getTransaction().commit();
            return member;
        } catch (Exception e) {
            throw e;
        }
    }
	
	public Member add(Member member) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.save(member);
		session.getTransaction().commit();
		return member;
	}
	public Member getMemberById(Integer id)
	{
		try {
        	Session session = HibernateUtil.getSessionFactory().getCurrentSession();    	 
        	session.beginTransaction();
        	Member member = null;                                
        	member = (Member) session.createCriteria(Member.class)
                    .add(Restrictions.eq("id",id))
                    .uniqueResult();
        	session.getTransaction().commit();
            return member;                       
        } catch (Exception e)
        {
            throw e;
        }
	}
	public Member UpdateAccount(Member member)
	{
		try{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();    	 
    	session.beginTransaction();
    	@SuppressWarnings("unchecked")
    	Query query = session.createQuery("update Member M set fullname = '"+member.getFullname()+
    			"' ,M.user = '"+member.getUser()+
    			"' ,M.pass = '"+member.getPass()+
    			"' ,M.id_card = '"+member.getId_card()+
    			"' ,M.address = '"+member.getAddress()+
    			"' ,M.phone = '"+member.getPhone()+
    			"' ,M.mail = '"+member.getMail()+"' where M.id = '" +member.getId()+"'");
    	
    	 query.executeUpdate();	
    	 member = getMemberById(member.getId());
    	 return member;    	     	    	
		}
		catch(Exception e)
		{
			throw e;
		}
		
	}
	public List<Member> getListmember() {
		return listmember;
	}

	public void setListmember(List<Member> listmember) {
		this.listmember = listmember;
	}

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}
	
}

